//
// Created by 14590 on 25-9-2.
//

#ifndef KEY_DRIVER_H
#define KEY_DRIVER_H

#include "stm32f4xx.h"
#include "cmsis_os2.h"
#include "gpio.h"

#define RowGPIOx GPIOE
#define ColGPIOx GPIOB

#define Key_F1 KeyState[4]&0x01
#define Key_F2 KeyState[5]&0x01
#define Key_F3 KeyState[6]&0x01
#define Key_F4 KeyState[7]&0x01
#define Key_F5 (KeyState[7]>>3)&0x01
#define Key_F6 (KeyState[7]>>1)&0x01/// #

#define Key_0 (KeyState[7]>>2)&0x01
#define Key_1 (KeyState[6]>>3)&0x01
#define Key_2 (KeyState[6]>>2)&0x01
#define Key_3 (KeyState[6]>>1)&0x01
#define Key_4 (KeyState[5]>>3)&0x01
#define Key_5 (KeyState[5]>>2)&0x01
#define Key_6 (KeyState[5]>>1)&0x01
#define Key_7 (KeyState[4]>>3)&0x01
#define Key_8 (KeyState[4]>>2)&0x01
#define Key_9 (KeyState[4]>>1)&0x01

#define Key_UP (KeyState[1]>>2)&0x01
#define Key_DOWN (KeyState[3]>>2)&0x01
#define Key_LEFT (KeyState[2]>>3)&0x01
#define Key_RIGHT (KeyState[2]>>1)&0x01
#define Key_MID (KeyState[2]>>2)&0x01

#define Key_All (KeyState[1]|KeyState[2]|KeyState[3]|KeyState[4]|KeyState[5]|KeyState[6]|KeyState[7])


#define LastKey_F1 KeyStateLast[4]&0x01
#define LastKey_F2 KeyStateLast[5]&0x01
#define LastKey_F3 KeyStateLast[6]&0x01
#define LastKey_F4 KeyStateLast[7]&0x01
#define LastKey_F5 (KeyStateLast[7]>>3)&0x01
#define LastKey_F6 (KeyStateLast[7]>>1)&0x01/// #

#define LastKey_0 (KeyStateLast[7]>>2)&0x01
#define LastKey_1 (KeyStateLast[6]>>3)&0x01
#define LastKey_2 (KeyStateLast[6]>>2)&0x01
#define LastKey_3 (KeyStateLast[6]>>1)&0x01
#define LastKey_4 (KeyStateLast[5]>>3)&0x01
#define LastKey_5 (KeyStateLast[5]>>2)&0x01
#define LastKey_6 (KeyStateLast[5]>>1)&0x01
#define LastKey_7 (KeyStateLast[4]>>3)&0x01
#define LastKey_8 (KeyStateLast[4]>>2)&0x01
#define LastKey_9 (KeyStateLast[4]>>1)&0x01

#define LastKey_UP (KeyStateLast[1]>>2)&0x01
#define LastKey_DOWN (KeyStateLast[3]>>2)&0x01
#define LastKey_LEFT (KeyStateLast[2]>>3)&0x01
#define LastKey_RIGHT (KeyStateLast[2]>>1)&0x01
#define LastKey_MID (KeyStateLast[2]>>2)&0x01

#define LastKey_All (KeyStateLast[1]|KeyStateLast[2]|KeyStateLast[3]|KeyStateLast[4]|KeyStateLast[5]|KeyStateLast[6]|KeyStateLast[7])


#define Key_F1_Down     Get_KeyIsAllowed(&Key[4][0])
#define Key_F2_Down     Get_KeyIsAllowed(&Key[5][0])
#define Key_F3_Down     Get_KeyIsAllowed(&Key[6][0])
#define Key_F4_Down     Get_KeyIsAllowed(&Key[7][0])
#define Key_F5_Down     Get_KeyIsAllowed(&Key[7][3])
#define Key_F6_Down     Get_KeyIsAllowed(&Key[7][1])

#define Key_0_Down      Get_KeyIsAllowed(&Key[7][2])
#define Key_1_Down      Get_KeyIsAllowed(&Key[6][3])
#define Key_2_Down      Get_KeyIsAllowed(&Key[6][2])
#define Key_3_Down      Get_KeyIsAllowed(&Key[6][1])
#define Key_4_Down      Get_KeyIsAllowed(&Key[5][3])
#define Key_5_Down      Get_KeyIsAllowed(&Key[5][2])
#define Key_6_Down      Get_KeyIsAllowed(&Key[5][1])
#define Key_7_Down      Get_KeyIsAllowed(&Key[4][3])
#define Key_8_Down      Get_KeyIsAllowed(&Key[4][2])
#define Key_9_Down      Get_KeyIsAllowed(&Key[4][1])

#define Key_UP_Down     Get_KeyIsAllowed(&Key[1][2])
#define Key_DOWN_Down   Get_KeyIsAllowed(&Key[3][2])
#define Key_LEFT_Down   Get_KeyIsAllowed(&Key[2][3])
#define Key_RIGHT_Down  Get_KeyIsAllowed(&Key[2][2])
#define Key_MID_Down    Get_KeyIsAllowed(&Key[2][1])
#define Key_All_Down    Get_AllKeyIsAllowed()


typedef struct {
    uint8_t State_Base;
    uint8_t State_Bias;
    uint8_t Allowed;

}Key_TypeDef;

extern Key_TypeDef Key[8][4];

void KeyInit();

uint8_t Get_KeyIsAllowed(Key_TypeDef *Key);
uint8_t Get_AllKeyIsAllowed();

extern uint8_t KeyState[8];
extern uint8_t KeyStateLast[8];

#endif //KEY_DRIVER_H
